package cn.edu.zut.hkxy.service.impl;

import cn.edu.zut.hkxy.mapper.StuinfoMapper;
import cn.edu.zut.hkxy.pojo.PageBean;
import cn.edu.zut.hkxy.pojo.Stuinfo;
import cn.edu.zut.hkxy.pojo.StuinfoExample;
import cn.edu.zut.hkxy.service.StuinfoService;
import cn.edu.zut.hkxy.util.DateFormat;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StuinfoServiceImpl implements StuinfoService {
    @Autowired
    private StuinfoMapper mapper;
    @Override
    public boolean add(Stuinfo stuinfo) {
        //获取时间
        Date date=new Date();
        stuinfo.setDate(date);
        stuinfo.setTime(date);
        stuinfo.setDatetimestr(DateFormat.formatToString(date));
        //点击量设置0
        stuinfo.setHits(0);
        if(mapper.insert(stuinfo)==1){
            return true;
        }
        return false;
    }

    @Override
    public List<Stuinfo> getPage(Integer pageNum, String state, String date, String title) {
        StuinfoExample example=new StuinfoExample();
        StuinfoExample.Criteria criteria=example.createCriteria();
        if(!state.equals("")){
            criteria.andStateEqualTo(Integer.parseInt(state));
        }
        if(!date.equals("")){
            criteria.andDateEqualTo(DateFormat.formatToDateOnlyDate(date));
        }
        if(!title.equals("")){
            criteria.andTitleLike("%"+title+"%");
        }
        example.setOrderByClause("datetimestr desc");
        //直接分页：第pageNum页，每页10条数据

        PageHelper.startPage(pageNum,10);
        List<Stuinfo> list=mapper.selectByExample(example);
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        if(mapper.deleteByPrimaryKey(id)==1){
           return true;
        }
        return false;
    }

    @Override
    public boolean modify(Stuinfo stuinfo) {
        //获取时间
        Date date=new Date();
        stuinfo.setDate(date);
        stuinfo.setTime(date);
        stuinfo.setDatetimestr(DateFormat.formatToString(date));
        if(mapper.updateByPrimaryKeySelective(stuinfo)==1){
            return true;
        }
        return false;
    }

    @Override
    public Stuinfo getById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean hitsAdd(Integer id) {
        Stuinfo stuinfo=this.getById(id);
        stuinfo.setHits(stuinfo.getHits()+1);
        mapper.updateByPrimaryKeySelective(stuinfo);
        return true;
    }

    @Override
    public Integer count() {
        mapper.countByExample(new StuinfoExample());
        return null;
    }

    @Override
    public PageBean getPageBean(int currentPageNum,int pageSize) {
        StuinfoExample example=new StuinfoExample();
        example.createCriteria().andStateEqualTo(1);
        example.setOrderByClause("datetimestr desc");
        PageHelper.startPage(currentPageNum,pageSize);
        List<Stuinfo> list=mapper.selectByExample(example);
        int count=(int)mapper.countByExample(example);
        PageBean pageBean=new PageBean(currentPageNum,pageSize,count,list);
        return pageBean;
    }
}
